package com.example.mymusicdisplay;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.example.mymusicdisplay.data.MusicData;
import com.example.mymusicdisplay.service.PlayMusicService;
import com.example.mymusicdisplay.utils.LiveDataBus;

import java.util.ArrayList;
import java.util.List;

public class MusicManager {
    private static final String TAG = "MusicManager";

    private volatile static MusicManager mMusicManager;
    private PlayMusicService mPlayMusicService;

    public MusicManager() {
    }

    public static MusicManager getInstance() {
        if (mMusicManager == null) {
            synchronized (MusicManager.class) {
                if (mMusicManager == null) {
                    mMusicManager = new MusicManager();
                }
            }
        }
        return mMusicManager;
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mPlayMusicService = ((PlayMusicService.PlayMusicBinder) iBinder).getService();
            mPlayMusicService.setMusicListener(mMusicListener);
            Log.d(TAG, "onServiceConnected: mPlayMusicService = " + mPlayMusicService);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mPlayMusicService = null;
        }
    };

    PlayMusicService.MusicListener mMusicListener = new PlayMusicService.MusicListener() {

        @Override
        public void onPlayStateChanged(boolean status) {
            Log.d(TAG, "onPlayStateChanged: status = " + status);
            if (mMusicChangedListeners.size() > 0) {
                for (MusicChangedListener musicChangedListener : mMusicChangedListeners) {
                    musicChangedListener.musicStatusChanged(status);
                }
            }
        }

        @Override
        public void onPositionChanged(MusicData musicData, int position) {
            Log.d(TAG, "onPlayStateChanged: musicData = " + musicData + " position = " + position);
            if (mMusicChangedListeners.size() > 0) {
                for (MusicChangedListener musicChangedListener : mMusicChangedListeners) {
                    musicChangedListener.musicPositionChanged(musicData);
                    LiveDataBus.get().with("position").postValue(position);
                }
            }
        }

        @Override
        public void onProgressChanged(int progress) {
            if (mMusicChangedListeners.size() > 0) {
                for (MusicChangedListener musicChangedListener : mMusicChangedListeners) {
                    musicChangedListener.musicProgressChanged(progress);
                }
            }
        }
    };

    public void bindMusicService(Context context) {
        Intent intent = new Intent(context, PlayMusicService.class);
        context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    private List<MusicChangedListener> mMusicChangedListeners = new ArrayList<>();

    public void registerListener(MusicChangedListener musicListener) {
        mMusicChangedListeners.add(musicListener);
    }

    public void unregisterLister(MusicChangedListener musicChangedListener) {
        mMusicChangedListeners.remove(musicChangedListener);
    }

    public interface MusicChangedListener {
        //当前音乐播放状态
        void musicStatusChanged(boolean status);

        //当前播放音乐
        void musicPositionChanged(MusicData musicData);

        //当前播放音乐进度
        void musicProgressChanged(int position);
    }

    public void nextMusic() {
        Log.d(TAG, "nextMusic: ");
        if (mPlayMusicService != null) {
            mPlayMusicService.next();
        } else {
            Log.e(TAG, "initMusicList: mPlayMusicService is null");
        }
    }

    public void beforeMusic() {
        Log.d(TAG, "beforeMusic: ");
        if (mPlayMusicService != null) {
            mPlayMusicService.before();
        } else {
            Log.e(TAG, "initMusicList: mPlayMusicService is null");
        }
    }

    public void pauseMusic() {
        Log.d(TAG, "pauseMusic: ");
        if (mPlayMusicService != null) {
            mPlayMusicService.pause();
        } else {
            Log.e(TAG, "initMusicList: mPlayMusicService is null");
        }
    }

    public void playMusic() {
        Log.d(TAG, "playMusic: ");
        if (mPlayMusicService != null) {
            mPlayMusicService.start();
        } else {
            Log.e(TAG, "initMusicList: mPlayMusicService is null");
        }
    }

    public void initMusicList(List<MusicData> musicDataList) {
        Log.d(TAG, "initMusicList: musicDataList = " + musicDataList);
        if (mPlayMusicService != null) {
            mPlayMusicService.setMusicList(musicDataList);
        } else {
            Log.e(TAG, "initMusicList: mPlayMusicService is null");
        }
    }

    public void playPositionMusic(int position) {
        Log.d(TAG, "playPositionMusic: position = " + position);
        if (mPlayMusicService != null) {
            mPlayMusicService.setPosition(position);
            mPlayMusicService.play();
        } else {
            Log.e(TAG, "initMusicList: mPlayMusicService is null");
        }
    }
}
